package com.stephentuso.welcome;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;

/**
 * Created by stephentuso on 11/16/15.
 */
/* package */ class ColorChangingBackgroundView extends Component implements Component.DrawTask {

    private BackgroundColor[] mColors = new BackgroundColor[0];

    private int mCurrentPosition = 0;
    private float mOffset = 0;

    private Paint mPaint = null;
    private Rect mRect = new Rect();

    public ColorChangingBackgroundView(Context context) {
        super(context);
        addDrawTask(this);
    }

    public ColorChangingBackgroundView(Context context, AttrSet attrs) {
        super(context, attrs);
        addDrawTask(this);
    }

    public ColorChangingBackgroundView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addDrawTask(this);
    }

    public void setPosition(int position, float offset) {
        mCurrentPosition = position;
        mOffset = offset;
        invalidate();
    }

    public void setColors(BackgroundColor[] colors) {
        mColors = colors;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mPaint == null) {
            mPaint = new Paint();
        }

        if (mColors == null || mColors.length == 0)
            return;
        mRect.left = getScrollValue(AXIS_X);
        mRect.top = getScrollValue(AXIS_Y);
        mRect.right = getScrollValue(AXIS_X) + (getRight() - getLeft());
        mRect.bottom = getScrollValue(AXIS_Y) + (getBottom() - getTop());
        mPaint.setColor(new Color(mColors[mCurrentPosition].value()));
        mPaint.setAlpha(1);
        canvas.drawRect(mRect, mPaint);

        if (mCurrentPosition != mColors.length - 1) {
            mPaint.setColor(new Color(mColors[mCurrentPosition + 1].value()));
            mPaint.setAlpha(mOffset);
            canvas.drawRect(mRect, mPaint);
        }

    }
}
